    import {defineStore} from 'pinia';
    import axios from "axios";
    import {useProductStore, useUserStore} from "@/stores/index.js";
    import {message} from "ant-design-vue";

    export const useFavoriteStore = defineStore('favorite', {
        state: () => ({
            favoriteItems: [],
        }),
        actions: {
            async fetchFavoriteItems() {
                const userId = useUserStore().userInfo ? useUserStore().userInfo.userId:null;
                if (!userId) {
                    message.error("请先登录")
                    return;
                }
                try {
                    const response = await axios.post('/api/favorite/getUserFavorite', {userId: userId});
                    if (response.data.code === 200) {
                        console.log(response.data.data);
                        this.favoriteItems = response.data.data;
                    }
                } catch (error) {
                    console.error('获取收藏失败', error);
                }
            },
            async toggleProductInFavorite(product) {
                const existingItem = this.cartItems.find(item => item.productId === product.productId);
                if (!useUserStore().userInfo.userId) {
                    message.error("请先登录");
                    return;
                }
                if (existingItem) {
                    // 如果商品已在收藏中，则移除
                    return this.removeProductFromCart(product.productId);
                } else {
                    // 如果商品不在收藏中，则添加
                    return this.addProductToCart(product);
                }
            },
        }
    });